package medSon;

import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class MedSonUpdateServlet
 */
@WebServlet("/MedSonUpdateServlet")
public class MedSonUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MedSonUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		String id=request.getParameter("id");
		DBTools db=new DBTools();
		db.open();
		String sql="SELECT msi.id,msi.name,mti.typeid,mti.typeName,mti.type FROM medTypeInfo mti LEFT JOIN medSonInfo msi ON mti.typeid=msi.typeid WHERE msi.id="+id;
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				MedSonClassifyBean mscb=new MedSonClassifyBean(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
				al.add(mscb);
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		db.close();
		MedSonClassifyBean mscb=new MedSonClassifyBean();
		mscb=(MedSonClassifyBean) al.get(0);
		request.setAttribute("id", mscb.getId());
		request.setAttribute("typeid", mscb.getTypeid());
		request.setAttribute("TypeName", mscb.getTypeName());
		request.setAttribute("name", mscb.getName());
		request.getRequestDispatcher("/medSonClassifyUpdate.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("names");
		int typeid=Integer.parseInt(request.getParameter("typeid"));
		int i=1;
		int j=0;
		DBTools db=new DBTools();
		db.open();
		String sqll="UPDATE medSonInfo SET name='"+name+"' WHERE id="+id;
		db.update(sqll);
		String sql="SELECT msi.id,msi.name,mti.typeid,mti.typeName,mti.type FROM medTypeInfo mti LEFT JOIN medSonInfo msi ON mti.typeid=msi.typeid WHERE mti.typeid="+typeid;
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				MedSonClassifyBean mscb=new MedSonClassifyBean(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5));
				al.add(mscb);
			}
			if(al.size()%5!=0){
				j=al.size()/5+1;
				if(i<1){
					i=al.size()/5+1;
				}
				if(i>al.size()/5+1){
					i=1;
				}
			}else{
				j=al.size()/5;
				
				if(i<1){
					i=al.size()/5;
				}
				if(i>al.size()/5){
					i=1;
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql2="SELECT msi.id,msi.name,mti.typeid,mti.typeName,mti.type FROM medTypeInfo mti LEFT JOIN medSonInfo msi ON mti.typeid=msi.typeid WHERE mti.typeid="+typeid+" ORDER BY mti.typeid DESC limit "+(i-1)*5+","+"5";
		ResultSet rss=db.select(sql2);
		ArrayList al1=new ArrayList();
		try {
			while(rss.next()){
				MedSonClassifyBean mscb=new MedSonClassifyBean(rss.getInt(1),rss.getString(2),rss.getInt(3),rss.getString(4),rss.getString(5));
				al1.add(mscb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		MedSonClassifyBean mscb=(MedSonClassifyBean) al1.get(0);
		request.setAttribute("al", al1);
		request.setAttribute("typeName", mscb.getTypeName());
		request.setAttribute("typeid", mscb.getTypeid());
		request.setAttribute("i", i);
		request.setAttribute("j", j);
		request.getRequestDispatcher("/medSonClassify.jsp").forward(request, response);
	}

}
